package com.lantosec.hmrp.modules.workflow.controller;

import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.datacorrection.CorrectionService;
import com.lantosec.hmrp.modules.workflow.vo.FlowDeploymentVO;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowDeployService;
import com.lantosec.hmrp.modules.workflow.service.IWorkflowRepositoryService;
import com.lantosec.hmrp.modules.workflow.vo.FlowProcdefVO;
import com.lantosec.hmrp.modules.workflow.vo.ModelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 流程部署控制器
 *
 * @author YCY
 * @since 2020-03-04
 */
@Api(value = "流程部署控制器", tags = "流程部署控制器")
@RestController
@RequestMapping("/workflow/deploy/")
@Log4j2
public class WorkflowDeployController {
    @Autowired
    private IWorkflowRepositoryService repositoryService;
    @Autowired
    private IWorkflowDeployService deployService;
    @Autowired
    private CorrectionService correctionService;

    @GetMapping("all")
    @ApiOperation(value = "查询全部部署的流程定义")
    public Result<PageVO<FlowDeploymentVO>> all(@ApiParam(value = "分页对象", required = true) PageVO page,
                                                @ApiParam(value = "部署的流程名称") String modelName,
                                                @ApiParam(value = "开始日期(yyyy-MM-dd)") String startDate,
                                                @ApiParam(value = "结束日期(yyyy-MM-dd)") String endDate) {
        try {
            return Result.ok(repositoryService.deploymentList(page, modelName, startDate, endDate));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("getProcessList")
    @ApiOperation(value = "最新流程部署列表")
    public Result<List<FlowProcdefVO>> getProcessList() {
        try {
            return Result.ok(repositoryService.processDefinitionList());
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("getByKey/{definitionKey}")
    @ApiOperation(value = "根据流程定义key获取最新的流程定义")
    public Result<FlowProcdefVO> getProcessDefinitionByKey(@ApiParam(value = "流程定义key", required = true)
                                                                   @PathVariable(name = "definitionKey")String definitionKey) {
        try {
            return Result.ok(repositoryService.getProcessDefinitionByKey(definitionKey));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("save/{id}")
    @ControllerLog(name = "工作流部署流程定义", type = LogType.update)
    @ApiOperation(value = "部署流程定义")
    public Result<String> deployment(@ApiParam(value = "流程模型ID", required = true) @PathVariable(name = "id") String modelId) {
        try {
            Deployment deployment = deployService.deploymentFromModelId(modelId);
            return Result.ok(deployment.getId());
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @DeleteMapping("del/{id}")
    @ControllerLog(name = "工作流删除部署的流程", type = LogType.delete)
    @ApiOperation(value = "删除部署的流程")
    public Result<Void> del(@ApiParam(value = "流程部署ID", required = true) @PathVariable(name = "id") String deploymentId) {
        try {
            repositoryService.deleteDeployment(deploymentId);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("activate/{id}")
    @ControllerLog(name = "工作流激活流程定义", type = LogType.update)
    @ApiOperation(value = "激活流程定义")
    public Result<String> activate(@ApiParam(value = "流程部署ID", required = true) @PathVariable(name = "id") String deploymentId) {
        try {
            repositoryService.activateProcessDefinitionById(deploymentId);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("suspend/{id}")
    @ControllerLog(name = "工作流挂起流程定义", type = LogType.update)
    @ApiOperation(value = "挂起流程定义")
    public Result<String> suspend(@ApiParam(value = "流程部署ID", required = true) @PathVariable(name = "id") String deploymentId) {
        try {
            repositoryService.suspendProcessDefinitionById(deploymentId);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("get/{deploymentId}")
    @ApiOperation(value = "根据部署Id查询部署信息")
    public Result<ModelVO> get(@ApiParam(value = "部署Id", required = true) @PathVariable(name = "deploymentId") String deploymentId) {
        try {
            ModelVO modelVO = deployService.getDeploymentInfo(deploymentId);
            return Result.ok(modelVO);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("correction")
    @ApiOperation(value = "修正流程的XML的部门Id")
    public Result<ModelVO> correction() {
        try {
            correctionService.execute();
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
}
